01. C++ Vectors
Vectors and Matrix Algebra
Great work! You made it through a bunch of C++ syntax. As you've probably realized, programming in C++ is arguably harder than programming in Python. C++ was designed for fast execution, and the language gives you a lot of different ways to get the same results. Python was designed for writing code quickly but at the expense of execution speed.
There is one last piece of syntax you'll need to translate your Python code from earlier in the nanodegree: C++ vectors, which are like Python lists.
The Vector Library
When you were writing Python programs to store and manipulate matrices, you used Python lists. C++ vectors are just like Python lists. In this lesson, you are going to practice using C++ vectors in preparation for translating Python code to C++.
But hold on! C++ also has something called a list. But this is where things get confusing. However, C++ lists do not work the same way as Python lists.
C++ lists and C++ vectors are both in a family of structures called sequence containers. These containers allow you to store values in series and then access those values. C++ has a handful of sequence containers including lists, vectors, and arrays.
Don't get confused! C++ vectors are the closest to Python lists. You can add elements to a C++ vector just like you can in a Python list. You can remove elements as well and also easily access any element in the vector.
Declaring C++ Vectors
Declaring C++ vector variables is like declaring any other type of variable:
typedefinition variablename;
But the vector type definition has a funny looking syntax because you also need to declare what kind of values will go inside the vector such as integer, char, float, string, etc. Here are some examples of variable declarations using vectors:
std::vector<char> charactervectorvariable;
std::vector<int> integervectorvariable;
std::vector<float> floatvectorvariable;
std::vector<double> doublevectorvariable:
Including the Vector Library
In an actual program, you would need to include the vector file from the Standard Library:
#include <vector>
int main() {
std::vector<float> floatvectorvariable;
return 0;
}
The above code will declare an empty vector of type float.
More generically, you declare a vector with:
std::vector<datatype> variablename;
Practice Declaring Vectors
Start Quiz:
// TODO: import the vector library
// TODO: write a program that declares three integer vectors named:
// vector1
// vector2
// vector3
#include <vector>
int main() {
std::vector<int> vector1;
std::vector<int> vector2;
std::vector<int> vector3;
return 0;
}
User's Answer:
(Note: The answer done by the user is not guaranteed to be correct)
// TODO: import the vector library
// TODO: write a program that declares three integer vectors named:
// vector1
// vector2
// vector3
#include <vector>
int main() {
std::vector<int> vector1;
std::vector<int> vector2;
std::vector<int> vector3;
return 0;
}
#include <vector>
int main() {
std::vector<int> vector1;
std::vector<int> vector2;
std::vector<int> vector3;
return 0;
}